package com.macro.mall.commission.service.impl;

import com.macro.mall.boot.wechat.mp.WxMpConfig;
import com.macro.mall.commission.entity.OrderPredicateEntity;
import com.macro.mall.commission.event.OrderPredicateEvent;
import com.macro.mall.commission.service.OrderPredicateService;
import com.macro.mall.common.api.util.BigDecimalUtils;
import com.macro.mall.ums.entity.WxMemberEntity;
import com.macro.mall.ums.enums.UserSourceTypeEnum;
import com.macro.mall.ums.service.account.WxMemberService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * class
 *
 * @author xuyanjun
 * @date 2020-01-20
 */
@Slf4j
@Component
public class OrderPredicateEventListener {

    @Autowired
    private OrderPredicateService orderPredicateService;

    @Autowired
    private WxMemberService wxMemberService;

    @EventListener(OrderPredicateEvent.class)
    public void onListener(OrderPredicateEvent event) {
        try {
            List<OrderPredicateEntity> orderPredicateEntities = orderPredicateService.listByBatchIds(event.getArg().getPredicateIds());
            sendOrderPredictTemplate(orderPredicateEntities);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    private void sendOrderPredictTemplate(List<OrderPredicateEntity> orderPredicateEntities) throws WxErrorException {
        Map<Long, List<OrderPredicateEntity>> groups = orderPredicateEntities.stream().collect(Collectors.groupingBy(OrderPredicateEntity::getUserId));

        for (Map.Entry<Long, List<OrderPredicateEntity>> group : groups.entrySet()) {
            WxMemberEntity wxMemberEntity = wxMemberService.getByMemberId(group.getKey());
            if (wxMemberEntity == null) {
                continue;
            }

            int amount = group.getValue().stream().mapToInt(OrderPredicateEntity::getAmount).sum();

            List<WxMpTemplateData> wxMpTemplateDataList = new ArrayList<>();
            wxMpTemplateDataList.add(new WxMpTemplateData("first", "新收益到账了～"));
            wxMpTemplateDataList.add(new WxMpTemplateData("keyword1", BigDecimalUtils.fromFengToYuan(amount, 2).toString()));
            wxMpTemplateDataList.add(new WxMpTemplateData("keyword2", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))));
            wxMpTemplateDataList.add(new WxMpTemplateData("remark", "继续努力吧！"));

            WxMpTemplateMessage wxMpTemplateMessage = new WxMpTemplateMessage();
            wxMpTemplateMessage.setData(wxMpTemplateDataList);
            wxMpTemplateMessage.setToUser(wxMemberEntity.getWxOpenId());
            wxMpTemplateMessage.setTemplateId("EjTFgO-vP_aRx77D8oJJ_A45See5b3zrUdvxQntnspU");

            WxMpConfig.getWxMpServiceByBusinessAppId(UserSourceTypeEnum.WX_MP.getValue())
                    .getTemplateMsgService()
                    .sendTemplateMsg(wxMpTemplateMessage);
        }
    }


}
